package com.kingint.web.portal.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.bas.entity.BasHotel;
import com.kingint.mbg.data.entity.DataCustomerPreference;
import com.kingint.mbg.data.entity.DataRoomHygiene;
import com.kingint.mbg.data.mapper.DataCustomerPreferenceMapper;
import com.kingint.mbg.data.mapper.DataWorkOrderMapper;
import com.kingint.mbg.dto.data.DataBackParam;
import com.kingint.mbg.dto.data.DataFrontBack;
import com.kingint.mbg.dto.data.DataParam;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasHotelService;
import com.kingint.web.portal.data.service.DataCustomerPreferenceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.RedisService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.DateUtil;
import com.kingint.web.util.GetDateUtils;
import com.kingint.web.vo.CountParam;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客人偏好统计表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2021-01-23
 */
@Service
public class DataCustomerPreferenceServiceImpl extends ServiceImpl<DataCustomerPreferenceMapper, DataCustomerPreference> implements DataCustomerPreferenceService {

    @Resource
    private RedisService redisService;
    @Resource
    private BasHotelService basHotelService;
    @Resource
    private DataWorkOrderMapper dataWorkOrderMapper;
    @Resource
    private UmsAdminService adminService;

    //定时1小时查询redis的服务工单，并写入数据库统计
//    @Scheduled(cron = "0 0 0/1 * * ?")
    private void timingSynchronization() {
        //查询所有酒店id
        List<BasHotel> list = basHotelService.list(null);

        list.forEach(v -> {
            Date date = new Date();
            String key = v.getId() + "_" + GetDateUtils.getHour(date) + "_" + CountParam.SERVICECOUNT;
            List<String> lrang = redisService.lrang(key, 0, -1); //当前酒店当前小时所有服务工单
            Map<String, Integer> countmap = null;
            //将redis中这个小时的数据按内容分类放入map
            lrang.forEach(k -> {
                if (ObjectUtils.isEmpty(countmap.get(k))) countmap.put(k, 1);
                else countmap.put(k, countmap.get(k) + 1);
            });

            countmap.forEach((k, m) -> {
                DataCustomerPreference dataCustomerPreference = new DataCustomerPreference();
                dataCustomerPreference.setTotalNum(m);
                dataCustomerPreference.setCreateTime(date);
                dataCustomerPreference.setHotelId(v.getId());
                dataCustomerPreference.setServiceClassId(Integer.parseInt(k.split("_")[0]));
                dataCustomerPreference.setServiceName(k.split("_")[1]);
                this.save(dataCustomerPreference);
            });


        });

    }

    /**
     * @return
     */
    @Override
    public Integer addRedisCustomerInfo(DataBackParam backParam) {
        //构成key
        Date date = new Date();
        String key = backParam.getHotelId() + "_" + GetDateUtils.getHour(date) + "_" + CountParam.SERVICECOUNT;
        String value=backParam.getServiceClassId()+"_"+backParam.getServiceClassName();
        Long lpush = redisService.lpush(key, value);
        return lpush.intValue();
    }

    @Override
    public Integer addDataHouseInfo(Integer hotelId,Integer serverId, String serviceName) {
        DataBackParam backParam = new DataBackParam();
        backParam.setHotelId(hotelId);
        backParam.setServiceClassId(serverId);
        backParam.setServiceClassName(serviceName);
        return addRedisCustomerInfo(backParam);
    }

    @Override
    public DataFrontBack getDataFrontBackInfo() {
        int hour = GetDateUtils.getHour();
        String[] x = new String[24]; //坐标x轴，小时
        Integer[] y = new Integer[24]; //坐标y轴，数量

        String[] x1 = new String[24]; //坐标x轴,服务内容
        Integer[] y1 = new Integer[24]; //坐标y轴，数量

        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();//酒店id
        Date date = new Date();
        //统计相关的数据
        //查询总的客房清洁次数
        //查询之前的客房清洁总数
        QueryWrapper<DataCustomerPreference> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id",hotelId).ge("create_time", GetDateUtils.getTimeString(GetDateUtils.FORMAT_SHORT));
        List<DataCustomerPreference> list = this.list(wrapper);
        int num=0; //当天总单数
        Integer time=0;  //当天总时间
        Map<String,Integer> map = null;
        int j = 0; //标志位
        if(!CollectionUtils.isEmpty(list)){
            for (DataCustomerPreference d :list) {
                num += d.getTotalNum(); //今天的所有数据总和（不包括当前小时），不能从总工单表拉取信息，总工单数据每天存储一次，今天只能获取昨天的
                x1[j] = d.getServiceName();
                y1[j] = d.getTotalNum();

                if (ObjectUtils.isEmpty(map.get(d.getServiceName()))){
                    map.put(d.getServiceName(),d.getTotalNum());
                }else{
                    map.put(d.getServiceName(),map.get(d.getServiceName()) + d.getTotalNum());
                }
                j++;
            }
        }


        //查询当前的所有的数据
        String key = hotelId+"_"+ GetDateUtils.getHour(date)+"_"+ CountParam.CLEANCOUNT;
        List<String> lrang = redisService.lrang(key, 0, -1); //当前酒店当前小时所有清洁工单
        if(!StringUtils.isEmpty(lrang)){
            num += lrang.size();  //总数加上当前小时的数量

            //计算当前小时的数据信息
            for (String info:lrang) {
                if (ObjectUtils.isEmpty(map.get(info))) map.put(info, 1);
                else map.put(info, map.get(info) + 1);
            }

        }

        j=0;//重置标志位
        //设置第二个表格
        for (String k : map.keySet()) {
            x[j] = k;
            y[j] = map.get(k);
        }

        //拼装数据
        DataFrontBack dataFrontBack = new DataFrontBack();
        dataFrontBack.setNum1(BigDecimal.valueOf(num)); //当天总和数量
        dataFrontBack.setNum2(BigDecimal.valueOf(time));//当天总和时间
        dataFrontBack.setNum3(BigDecimal.valueOf(lrang.size()));//当前小时总和
        dataFrontBack.setNum4(BigDecimal.valueOf(num/hour));//当前小时总和时间
        //构建坐标
        DataParam dataParam = new DataParam();
        dataParam.setTime(x);
        dataParam.setValue(y);

        DataParam dataParam1 = new DataParam();
        dataParam1.setTime(x1);
        dataParam1.setValue(y1);
        List<DataParam> list1 = new ArrayList<>();
        list1.add(dataParam);
        list1.add(dataParam1);
        dataFrontBack.setList(list1);//折线图

        return dataFrontBack;
    }
}
